Import cal3d

Class CalMorphTargetMixer

	Field m_model:CalModel
	Field m_vectorCurrentWeight:Float[]
	Field m_vectorEndWeight:Float[]
	Field m_vectorDuration:Float[]
	
	Method New()
	
		m_model = Null
		m_vectorCurrentWeight = New Float[0]
		m_vectorEndWeight = New Float[0]
		m_vectorDuration = New Float[0]		
	
	End
	
	Method New(model:CalModel)
	
		m_model = model
		Local morphAnimationCount:Int = model.getCoreModel().getCoreMorphAnimationCount()
		m_vectorCurrentWeight = New Float[morphAnimationCount]
		m_vectorEndWeight = New Float[morphAnimationCount]
		m_vectorDuration = New Float[morphAnimationCount]			
		
	End


	Method blend:Bool(id:Int, weight:Float, delay:Float)
	
		If id < 0 Or id >= m_vectorCurrentWeight.Length Then
			Print "Invalid Morph ID"
			Return False
		Endif
	
		m_vectorEndWeight[id] = weight
		m_vectorDuration[id] = delay
		Return True		
	
	End	
	
	
	Method clear:Bool(id:Int, delay:Float)
		If id < 0 Or id >= m_vectorCurrentWeight.Length Then
			Print "Invalid Morph ID"
			Return False
		Endif
	
		m_vectorEndWeight[id] = 0
		m_vectorDuration[id] = delay
		Return True		
	End
	
	Method getCurrentWeight:Float(id:Int)
		If id < 0 Or id >= m_vectorCurrentWeight.Length Then
			Print "Invalid Morph ID"
			Return False
		Endif
		
		Return m_vectorCurrentWeight[id]	
	End
	
	Method getCurrentWeightBase:Float()
	
		Local currentWeight:Float = 1
		For Local i:Int = 0 To m_vectorCurrentWeight.Length-1
			currentWeight -= m_vectorCurrentWeight[i]
		Next
	
		Return currentWeight
		
	End
	
	Method getMorphTargetCount:Int()
		Return m_vectorCurrentWeight.Length
	End
	
	Method update:Void(deltaTime:Float)
		For Local i:Int = 0 To m_vectorCurrentWeight.Length-1
			Local currentWeight:Float = m_vectorCurrentWeight[i]
			Local endWeight:Float = m_vectorEndWeight[i]
			Local duration:Float = m_vectorDuration[i]
	
			If deltaTime >= duration then
				m_vectorCurrentWeight[i] = endWeight
				m_vectorDuration[i] = 0
			Else
				m_vectorCurrentWeight[i] += (endWeight - currentWeight) * deltaTime / duration
				m_vectorDuration[i] -= deltaTime
			Endif
		Next
	
		For Local morphAnimationID:Int = 0 To m_vectorCurrentWeight.Length-1
			Local coreMorphAnimation:CalCoreMorphAnimation = m_model.getCoreModel().getCoreMorphAnimation(morphAnimationID)
	
			Local vectorCoreMeshID:Int[] = coreMorphAnimation.getVectorCoreMeshID()
			Local vectorMorphTargetID:Int[] = coreMorphAnimation.getVectorMorphTargetID()
	
			For Local i:Int = 0 To vectorCoreMeshID.Length-1
				Local vectorSubmesh:CalSubmesh[] = m_model.getMesh(vectorCoreMeshID[i]).getVectorSubmesh()
	
				Local submeshCount:Int = vectorSubmesh.Length
				For Local submeshId:Int = 0 To submeshCount-1
					vectorSubmesh[submeshId].setMorphTargetWeight(vectorMorphTargetID[i],  m_vectorCurrentWeight[morphAnimationID])
				Next
			Next
		Next	
	End
	
	
	
End Class